package com.im.service.projectInfo.smo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.im.viewmodel.fundPlan.FileVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;
import com.im.MDA;
import com.im.service.common.dao.IFileOperDAO;
import com.im.service.common.smo.ICommonSMO;
import com.im.service.dto.BaseException;
import com.im.service.dto.DocInfo;
import com.im.service.dto.FileUpload;
import com.im.service.dto.FundSource;
import com.im.service.dto.ImplementProject;
import com.im.service.dto.PlanDeclareSet;
import com.im.service.dto.PlanProject;
import com.im.service.dto.ProjProceSubmit;
import com.im.service.dto.ProjectStage;
import com.im.service.dto.Staff;
import com.im.service.dto.StageNode;
import com.im.service.planDeclareSet.dao.IPlanDeclareSetOperDAO;
import com.im.service.projectInfo.dao.IFundSourceOperDAO;
import com.im.service.projectInfo.dao.IImplementProjOperDAO;
import com.im.service.projectInfo.dao.IImplementProjQueryDAO;
import com.im.service.projectInfo.dao.IPlanProjectOperDAO;
import com.im.service.projectInfo.dao.IPlanProjectQueryDAO;
import com.im.util.DateUtil;
import com.im.util.ReturnsUtil;
import com.im.viewmodel.projectInfo.ProjectSetProgressAddReqVo;

/**
 * 项目信息管理 事务操作SMO
 * 
 * @author xuwb 2018年3月23日
 */
@Service
public class ProjectInfoOperSMOImpl implements IProjectInfoOperSMO {

	private static final Logger logger = LogManager.getLogger(ProjectInfoOperSMOImpl.class);

	@Autowired
	private IPlanProjectOperDAO planProjectOperDAO;
	
	@Autowired
	private IPlanProjectQueryDAO planProjectQueryDAO;

	@Autowired
	private IFundSourceOperDAO fundSourceOperDAO;
	@Autowired
	private IImplementProjOperDAO implementProjOperDAO;
	
	@Autowired
	private IPlanDeclareSetOperDAO planDeclareSetOperDAO;

	@Autowired
	private IImplementProjQueryDAO implementProjQueryDAO;
	
	@Autowired
    private ICommonSMO commonSMO;
	
	@Autowired
	private IFileOperDAO fileOperDAO;
	
	@Override
	public void addPlanProject(PlanProject planProject) throws Exception {
			// 1.新增规划工程
			String planProjId = UUID.randomUUID().toString();
			planProject.setPlanProjId(planProjId);
			planProject.setPlanProjCode(commonSMO.querySeqForRule("5",planProject.getPlanProjClass()));
			Double total = planProject.getFundSourceBu() + planProject.getFundSourceGao()
					+ planProject.getFundSourceQi() + planProject.getFundSourceSheng() + planProject.getFundSourceShi();
			planProject.setTotal(BigDecimal.valueOf(total));

			planProjectOperDAO.addPlanProject(planProject);

			// 2.新增资金信息
			FundSource fundSource = new FundSource();
			fundSource.setFundSourceObjType(MDA.FUND_SOURCE_OBJ_TYPE_PLAN);// 规划项目
			fundSource.setFundSourceObj(planProjId);
			fundSource.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
			fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
																					
			// 部补助
			if (planProject.getFundSourceBu() != null) {
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_BU);
				fundSource.setFundVal(planProject.getFundSourceBu().toString());
				fundSourceOperDAO.addFundSource(fundSource);
			}
			// 省财政
			if (planProject.getFundSourceSheng() != null) {
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHENG);
				fundSource.setFundVal(planProject.getFundSourceSheng().toString());
				fundSourceOperDAO.addFundSource(fundSource);
			}
			// 高速水运融资
			if (planProject.getFundSourceGao() != null) {
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_GAO);
				fundSource.setFundVal(planProject.getFundSourceGao().toString());
				fundSourceOperDAO.addFundSource(fundSource);
			}
			// 市州配套
			if (planProject.getFundSourceShi() != null) {
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHI);
				fundSource.setFundVal(planProject.getFundSourceShi().toString());
				fundSourceOperDAO.addFundSource(fundSource);
			}
			// 企业自筹
			if (planProject.getFundSourceQi() != null) {
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_QI);
				fundSource.setFundVal(planProject.getFundSourceQi().toString());
				fundSourceOperDAO.addFundSource(fundSource);
			}
	}

	@Override
	public void updatePlanProject(PlanProject planProject) throws Exception {
		try {
			// 1.更新规划项目信息
			Double total = planProject.getFundSourceBu() + planProject.getFundSourceGao()
					+ planProject.getFundSourceQi() + planProject.getFundSourceSheng() + planProject.getFundSourceShi();
			planProject.setTotal(BigDecimal.valueOf(total));
			planProjectOperDAO.updateByPrimaryKeySelective(planProject);
			
			// 2.更新资金信息
			FundSource fundSource = new FundSource();
			fundSource.setFundSourceObjType(MDA.FUND_SOURCE_OBJ_TYPE_PLAN);// 规划项目
			fundSource.setFundSourceObj(planProject.getPlanProjId());
			fundSource.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
			int num = 0;
			// 部补助
			if (planProject.getFundSourceBu() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_BU);
				fundSource.setFundVal(planProject.getFundSourceBu().toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 省财政
			if (planProject.getFundSourceSheng() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHENG);
				fundSource.setFundVal(planProject.getFundSourceSheng().toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 高速水运融资
			if (planProject.getFundSourceGao() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_GAO);
				fundSource.setFundVal(planProject.getFundSourceGao().toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 市州配套
			if (planProject.getFundSourceShi() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHI);
				fundSource.setFundVal(planProject.getFundSourceShi().toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 企业自筹
			if (planProject.getFundSourceQi() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_QI);
				fundSource.setFundVal(planProject.getFundSourceQi().toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(ReturnsUtil.getStackTrackMessage(e));
			}
			throw new Exception(ReturnsUtil.getStackTrackMessage(e));
		}
	}
	
	@Override
	public void deletePlanProj(PlanProject planProject) {
		//规划项目删除--作废
		planProject.setStatusCd(MDA.PLAN_PROJ_STATUS_CD_DELETE);
		if(StringUtil.isNotEmpty(planProject.getPlanProjIds())){
			String ids = planProject.getPlanProjIds();
			String[] idArray = ids.split(MDA.SEPARATOR);
			for(String id : idArray){
				planProject.setPlanProjId(id);
				planProjectOperDAO.updateByPrimaryKeySelective(planProject);
			}
		}
	}

	@Override
	public void addImplementProj(ImplementProject implProj) throws Exception {
		// 1.新增实施工程
		String impleProjId = UUID.randomUUID().toString();
		implProj.setImpleProjId(impleProjId);
		// 获取对应规划项目编码 为生成实施项目编码准备
		PlanProject pp = new PlanProject();
		pp.setPlanProjId(implProj.getPlanProjId());
		PlanProject resultPp =  planProjectQueryDAO.queryPlanProjDetail(pp);
		
		implProj.setImpleProjCode(resultPp.getPlanProjCode()+"-"+commonSMO.querySeqForRule("1", implProj.getPlanProjId()));
		Double total = implProj.getFundSourceBu() + implProj.getFundSourceGao()
				+ implProj.getFundSourceQi() + implProj.getFundSourceSheng() + implProj.getFundSourceShi();
		implProj.setTotal(BigDecimal.valueOf(total));

		implementProjOperDAO.addImplementProj(implProj);

		// 2.新增资金信息
		FundSource fundSource = new FundSource();
		fundSource.setFundSourceObjType(MDA.FUND_SOURCE_OBJ_TYPE_IMPLEMENT);// 实施项目
		fundSource.setFundSourceObj(impleProjId);
		fundSource.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
		fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
																				
		// 部补助
		if (implProj.getFundSourceBu() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_BU);
			fundSource.setFundVal(implProj.getFundSourceBu().toString());
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 省财政
		if (implProj.getFundSourceSheng() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHENG);
			fundSource.setFundVal(implProj.getFundSourceSheng().toString());
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 高速水运融资
		if (implProj.getFundSourceGao() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_GAO);
			fundSource.setFundVal(implProj.getFundSourceGao().toString());
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 市州配套
		if (implProj.getFundSourceShi() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHI);
			fundSource.setFundVal(implProj.getFundSourceShi().toString());
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 企业自筹
		if (implProj.getFundSourceQi() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_QI);
			fundSource.setFundVal(implProj.getFundSourceQi().toString());
			fundSourceOperDAO.addFundSource(fundSource);
		}
	}

	@Override
	public void updateImpleProj(ImplementProject implProj) {
		// 1.更新规划项目信息
		Double total = implProj.getFundSourceBu() + implProj.getFundSourceGao()
				+ implProj.getFundSourceQi() + implProj.getFundSourceSheng() + implProj.getFundSourceShi();
		implProj.setTotal(BigDecimal.valueOf(total));
		implementProjOperDAO.updateByPrimaryKeySelective(implProj);
		
		// 2.更新资金信息
		FundSource fundSource = new FundSource();
		fundSource.setFundSourceObjType(MDA.FUND_SOURCE_OBJ_TYPE_IMPLEMENT);// 实施项目
		fundSource.setFundSourceObj(implProj.getImpleProjId());
		fundSource.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
		int num = 0;
		// 部补助
		if (implProj.getFundSourceBu() != null) {
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_BU);
			fundSource.setFundVal(implProj.getFundSourceBu().toString());
			num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
			if(num == 0){
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
				fundSourceOperDAO.addFundSource(fundSource);
			}
		}
		// 省财政
		if (implProj.getFundSourceSheng() != null) {
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHENG);
			fundSource.setFundVal(implProj.getFundSourceSheng().toString());
			num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
			if(num == 0){
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
				fundSourceOperDAO.addFundSource(fundSource);
			}
		}
		// 高速水运融资
		if (implProj.getFundSourceGao() != null) {
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_GAO);
			fundSource.setFundVal(implProj.getFundSourceGao().toString());
			num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
			if(num == 0){
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
				fundSourceOperDAO.addFundSource(fundSource);
			}
		}
		// 市州配套
		if (implProj.getFundSourceShi() != null) {
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHI);
			fundSource.setFundVal(implProj.getFundSourceShi().toString());
			num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
			if(num == 0){
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
				fundSourceOperDAO.addFundSource(fundSource);
			}
		}
		// 企业自筹
		if (implProj.getFundSourceQi() != null) {
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_QI);
			fundSource.setFundVal(implProj.getFundSourceQi().toString());
			num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
			if(num == 0){
				fundSource.setFundSourceId(UUID.randomUUID().toString());
				fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
				fundSourceOperDAO.addFundSource(fundSource);
			}
		}		
	}

	@Override
	public void deleteImpleProj(ImplementProject implProj) {
		if(StringUtil.isNotEmpty(implProj.getImpleProjIds())){
			String ids = implProj.getImpleProjIds();
			String[] idArray = ids.split(MDA.SEPARATOR);
			for(String id : idArray){
				implProj.setImpleProjId(id);
				
				//1.查询子项目，级联删除子项目
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("impleProj", implProj);
				List<ImplementProject> subList = implementProjQueryDAO.querySubImpleProjList(param);
				for(ImplementProject sub : subList){
					sub.setStatusCd(MDA.IMPLE_PROJ_STATUS_CD_DELETE);
					implementProjOperDAO.updateByPrimaryKeySelective(sub);
				}
				
				//项目删除--作废
				implProj.setStatusCd(MDA.IMPLE_PROJ_STATUS_CD_DELETE);
				implementProjOperDAO.updateByPrimaryKeySelective(implProj);
			}
		}
	}

	@Override
	public void addProjStage(ProjectStage projStage) {
		// 0.项目前期工作阶段批复 一个项目只能有一个阶段为当前阶段
		if(MDA.PROJECT_STAGE_STATUS_CD_UNDERWAY.equals(projStage.getStatusCd())){
			List<ProjectStage> list = implementProjQueryDAO.queryImpleProjStageList(projStage);
			if(list != null && list.size() > 0){
				//535录入后续前期工作阶段时能自动将前置当前工作阶段置为否，减少来回操作次数
				//throw new BaseException("该项目已存在当前阶段!");
				for(ProjectStage projectStage : list){
					projectStage.setStatusCd(MDA.PROJECT_STAGE_STATUS_CD_FINISH);
					implementProjOperDAO.updateProjStage(projectStage);
				}
			}
		}
		
		String projStageId = UUID.randomUUID().toString();
		Double bu = projStage.getFundSourceBu() != null ? projStage.getFundSourceBu() : 0;
		Double gao = projStage.getFundSourceGao() != null ? projStage.getFundSourceGao() : 0;
		Double qi = projStage.getFundSourceQi() != null ? projStage.getFundSourceQi() : 0;
		Double sheng = projStage.getFundSourceSheng() != null ? projStage.getFundSourceSheng() : 0;
		Double shi = projStage.getFundSourceShi() != null ? projStage.getFundSourceShi() : 0;
		Double total = bu + gao + qi + sheng + shi;
		projStage.setTotal(BigDecimal.valueOf(total));
		projStage.setProjStageId(projStageId);
		projStage.setProjStageType(MDA.PROJECT_STAGE_TYPE_PRE_WORK);//前期工作阶段批复(PRE_WORK_APPROVALl)
		implementProjOperDAO.addProjStage(projStage);

		// 1.新增资金信息
		FundSource fundSource = new FundSource();
		fundSource.setFundSourceObjType(MDA.FUND_SOURCE_OBJ_TYPE_STAGE);// 项目阶段
		fundSource.setFundSourceObj(projStageId);
		fundSource.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
		fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
																				
		// 部补助
		if (projStage.getFundSourceBu() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_BU);
			fundSource.setFundVal(bu + "");
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 省财政
		if (projStage.getFundSourceSheng() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHENG);
			fundSource.setFundVal(sheng + "");
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 政府融资
		if (projStage.getFundSourceGao() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_GAO);
			fundSource.setFundVal(gao + "");
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 市州配套
		if (projStage.getFundSourceShi() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHI);
			fundSource.setFundVal(shi + "");
			fundSourceOperDAO.addFundSource(fundSource);
		}
		// 企业自筹
		if (projStage.getFundSourceQi() != null) {
			fundSource.setFundSourceId(UUID.randomUUID().toString());
			fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_QI);
			fundSource.setFundVal(qi + "");
			fundSourceOperDAO.addFundSource(fundSource);
		}
		
		
		// 2.附件 如果有上传附件则在t_file_upload中增加的【UPLOAD_OBJ_TYPE=90】【upload_obj_id=T_PROJECT_STAGE.projStageId】
		List<FileUpload> fileList = projStage.getFileList();
		if(fileList != null && fileList.size() > 0){
			for(FileUpload fileUpload : fileList){
				String uploadType = "";
				if (fileUpload.getFileSuffix() != null && fileUpload.getFileSuffix().length() > 0) {
					uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileUpload.getFileSuffix().toLowerCase());
				}
				String uploadId = UUID.randomUUID().toString();
				fileUpload.setUploadId(uploadId);
				fileUpload.setUploadObjType("90");
				fileUpload.setUploadObjId(projStage.getProjStageId());
				fileUpload.setUploadType(uploadType);
				fileOperDAO.saveFileUpload(fileUpload);
			}
		}
	}
	
	@Override
	public void uploadProjStageFile(ProjectStage projStage) {
		// 2.附件 如果有上传附件则在t_file_upload中增加的【UPLOAD_OBJ_TYPE=90】【upload_obj_id=T_PROJECT_STAGE.projStageId】
		List<FileUpload> fileList = projStage.getFileList();
		if(fileList != null && fileList.size() > 0){
			for(FileUpload fileUpload : fileList){
				String uploadType = "";
				if (fileUpload.getFileSuffix() != null && fileUpload.getFileSuffix().length() > 0) {
					uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileUpload.getFileSuffix().toLowerCase());
				}
				String uploadId = UUID.randomUUID().toString();
				fileUpload.setUploadId(uploadId);
				fileUpload.setUploadObjType("90");
				fileUpload.setUploadObjId(projStage.getProjStageId());
				fileUpload.setUploadType(uploadType);
				fileOperDAO.saveFileUpload(fileUpload);
			}
		}
	}

	@Override
	public void updateProjStage(ProjectStage projStage) {
		
		if(projStage.getStatusCd() != null && projStage.getStatusCd() == MDA.PROJECT_STAGE_STATUS_CD_DELETE){
			implementProjOperDAO.updateProjStage(projStage);
			// feat：删除当前阶段 是 时，将上一阶段设置为 当前阶段 是  modify by xuwb 20180803
			int projStageClass = Integer.parseInt(projStage.getProjStageClass());
			int preProjStageClass = projStageClass - 10;//获取上一个阶段
			if(preProjStageClass > 0){
				ProjectStage ps = new ProjectStage();
				ps.setImpleProjId(projStage.getImpleProjId());
				ps.setStatusCd(MDA.PROJECT_STAGE_STATUS_CD_UNDERWAY);
				ps.setProjStageType(MDA.PROJECT_STAGE_TYPE_PRE_WORK);
				ps.setProjStageClass(preProjStageClass + "");
				
				implementProjOperDAO.updateProjStage(ps);
			}
			
		}else{
			
			Double bu = projStage.getFundSourceBu() != null ? projStage.getFundSourceBu() : 0;
			Double gao = projStage.getFundSourceGao() != null ? projStage.getFundSourceGao() : 0;
			Double qi = projStage.getFundSourceQi() != null ? projStage.getFundSourceQi() : 0;
			Double sheng = projStage.getFundSourceSheng() != null ? projStage.getFundSourceSheng() : 0;
			Double shi = projStage.getFundSourceShi() != null ? projStage.getFundSourceShi() : 0;
			Double total = bu + gao + qi + sheng + shi;
			projStage.setTotal(BigDecimal.valueOf(total));
			
			implementProjOperDAO.updateProjStage(projStage);
			
			// 2.更新资金信息
			FundSource fundSource = new FundSource();
			fundSource.setFundSourceObjType(MDA.FUND_SOURCE_OBJ_TYPE_STAGE);
			fundSource.setFundSourceObj(projStage.getProjStageId());
			fundSource.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
			int num = 0;
			// 部补助
			if (projStage.getFundSourceBu() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_BU);
				fundSource.setFundVal(bu.toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 省财政
			if (projStage.getFundSourceSheng() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHENG);
				fundSource.setFundVal(sheng.toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 高速水运融资
			if (projStage.getFundSourceGao() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_GAO);
				fundSource.setFundVal(gao.toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 市州配套
			if (projStage.getFundSourceShi() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_SHI);
				fundSource.setFundVal(shi.toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			// 企业自筹
			if (projStage.getFundSourceQi() != null) {
				fundSource.setFundSourceType(MDA.FUND_SOURCE_TYPE_QI);
				fundSource.setFundVal(qi.toString());
				num = fundSourceOperDAO.updateFundSourceByObjId(fundSource);
				if(num == 0){
					fundSource.setFundSourceId(UUID.randomUUID().toString());
					fundSource.setYearDt(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));
					fundSourceOperDAO.addFundSource(fundSource);
				}
			}
			
			
			// 3. 如果有上传附件则在t_file_upload中增加的【UPLOAD_OBJ_TYPE=90】【upload_obj_id=T_PROJECT_STAGE.projStageId】
			List<FileUpload> fileList = projStage.getFileList();
			if(fileList != null && fileList.size() > 0){
				for(FileUpload fileUpload : fileList){
					String uploadType = "";
					if (fileUpload.getFileSuffix() != null && fileUpload.getFileSuffix().length() > 0) {
						uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileUpload.getFileSuffix().toLowerCase());
					}
					String uploadId = UUID.randomUUID().toString();
					fileUpload.setUploadId(uploadId);
					fileUpload.setUploadObjType("90");
					fileUpload.setUploadObjId(projStage.getProjStageId());
					fileUpload.setUploadType(uploadType);
					fileOperDAO.saveFileUpload(fileUpload);
				}
			}
		}
	}

	@Override
	public void addStageNode(StageNode stageNode) {
		String nodeId = UUID.randomUUID().toString();
		stageNode.setNodeId(nodeId);
		stageNode.setStatusCd(Integer.parseInt(MDA.EFFECT_STATUS_CD));
		
		implementProjOperDAO.addStageNode(stageNode);
	}

	@Override
	public void addDocInfo(DocInfo docInfo,HttpServletRequest request) {
		String docInfoId = UUID.randomUUID().toString();
		docInfo.setDocInfoId(docInfoId);
		docInfo.setDocSourceType(MDA.DOC_SOURCE_TYPE_STAGENODE);
		docInfo.setStatusCd(10);//存档
		//获取员工
		Object staffObj = request.getSession().getAttribute("loginedStaff");
		if(staffObj != null){
			Staff staff = (Staff) staffObj;
			docInfo.setCreater(staff.getStaffId());
		}
		
		implementProjOperDAO.addDocInfo(docInfo);
	}

	/**
	 * 实施项目报送
	 * 支持批量操作
	 */
	@Override
	public void updateProjPlanDeclareSet(ProjectSetProgressAddReqVo addReqVo) {
//		ImplementProject implProj = new ImplementProject();
//		implProj.setImpleProjId(addReqVo.getImpleProjId());
//		implProj.setIfSubmit(addReqVo.getIfSubmit());
//		implementProjOperDAO.updateByPrimaryKeySelective(implProj);
		
		if(StringUtil.isNotEmpty(addReqVo.getImpleProjIds())){
			String ids = addReqVo.getImpleProjIds();
			String[] idArray = ids.split(MDA.SEPARATOR);
			ImplementProject implProj = new ImplementProject();
			for(String id : idArray){
				implProj.setImpleProjId(id);
				implProj.setIfSubmit(addReqVo.getIfSubmit());
				implementProjOperDAO.updateByPrimaryKeySelective(implProj);
			}
		}
		
		
		
	}

	@Override
	public void deleteProjPlanDeclareSet(PlanDeclareSet planDs) {
		planDs.setStatusCd(MDA.DEL_STATUS);
		planDeclareSetOperDAO.updatePlanDeclareSet(planDs);
	}

	@Override
	public void updateProjProceSubmit(ProjProceSubmit projProceSubmit,HttpServletRequest request) {
		//1.查询记录是否存在
		int num = implementProjQueryDAO.queryProjProceSubmitByImpleId(projProceSubmit.getImpleProjId());
		//获取登录员工
		Object staffObj = request.getSession().getAttribute("loginedStaff");
		String staffId = "";
		if(staffObj != null){
			Staff staff = (Staff) staffObj;
			staffId = staff.getStaffId();
		}
		projProceSubmit.setSubmitter(staffId);
		projProceSubmit.setEnterDt(new Date());
		projProceSubmit.setAuditDt(new Date());
		projProceSubmit.setCurSituation(projProceSubmit.getImpleSituation());
		
		if(num > 0){
			//2.存在则更新
			projProceSubmit.setVersion(new Date());
			implementProjOperDAO.updateProjProceSubmit(projProceSubmit);
		}else{
			//3.不存在则新增
			projProceSubmit.setProceSubmitId(UUID.randomUUID().toString());
			projProceSubmit.setSubmitDt(new Date());
			projProceSubmit.setCreateDt(new Date());
			implementProjOperDAO.addProjProceSubmit(projProceSubmit);
		}
		
		// 4.更新项目 进展描述
		ImplementProject implProj = new ImplementProject();
		implProj.setImpleProjId(projProceSubmit.getImpleProjId());
		implProj.setImpleSituation(projProceSubmit.getImpleSituation());
		implementProjOperDAO.updateByPrimaryKeySelective(implProj);
		
		// 5.附件 上传附件
		List<FileVo> fileList = projProceSubmit.getFileList();
		if (fileList == null || fileList.size() == 0) {
			fileList = projProceSubmit.getAddFileList();
		}
		if(fileList != null && fileList.size() > 0){
			for(FileVo fileVo : fileList){
				FileUpload fileUpload = new FileUpload();
				String uploadType = "";
				if (fileVo.getAttachSuffix()!= null && fileVo.getAttachSuffix().length() > 0) {
					uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileVo.getAttachSuffix().toLowerCase());
				}
				String uploadId = UUID.randomUUID().toString();
				fileUpload.setUploadId(uploadId);
				fileUpload.setUploadObjType("20");
				fileUpload.setUploadObjId(projProceSubmit.getImpleProjId());
				fileUpload.setUploadType(uploadType);
				fileUpload.setUploadName(fileVo.getAttachFullName());
				fileUpload.setFilePath(fileVo.getAttachPath());
				fileOperDAO.saveFileUpload(fileUpload);
			}
		}
		
		List<FileVo> delList = projProceSubmit.getDelFileList();
		if (delList != null) {
			for (FileVo fileVo: delList) {
				FileUpload fileUpload = new FileUpload();
				fileUpload.setUploadId(fileVo.getUploadId());
				fileOperDAO.delFileUpload(fileUpload);
			}
		}
	}
}
